package com.feri.sh.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feri.sh.api.service.QuestionService;
import com.feri.sh.common.pojo.bo.QuestionAddBo;
import com.feri.sh.common.pojo.bo.QuestionUpdateBo;
import com.feri.sh.common.pojo.dto.QuesAnsDto;
import com.feri.sh.common.pojo.vo.R;
import com.feri.sh.core.dao.QuestionDao;
import com.feri.sh.core.entity.Question;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionDao,Question> implements QuestionService {
    @Resource
    private QuestionDao dao;
    @Override
    public R queryByUid(int uid) {
        List<Question> questions = dao.selectList(new LambdaQueryWrapper<Question>().eq(Question::getUid, uid));
        return R.ok(questions);
    }

    @Override
    public R add(QuestionAddBo bo) {
         if(bo!=null){
             Question question = new Question();
             BeanUtils.copyProperties(bo,question);
             question.setCtime(new Date());
             question.setCount(0);

             if(dao.insert(question)>0){
                 return R.ok();
             }else R.fail();
         }
        return R.ok("亲，请输入参数！");
    }

    @Override
    public R selectByQid(int id) {
        QuesAnsDto quesAnsDto = dao.selectById2(id);
        return R.ok(quesAnsDto);
    }

    @Override
    public R updateCount(QuestionUpdateBo bo) {
        Question existingQuestion = getById(bo.getId());
        if (existingQuestion != null) {
            BeanUtils.copyProperties(bo,existingQuestion);
            existingQuestion.setCount(existingQuestion.getCount() + 1);
            if (updateById(existingQuestion)) {
                return R.ok();
            } else {
                return R.fail("更新问题回答数量失败");
            }
        } else {
            return R.fail("问题不存在");
        }
    }
}
